"use strict";
var WebSocketServer = require('ws').Server;
var http = require('http');

var server = http.createServer();
var wss = new WebSocketServer({ server: server });

function broadcast(obj) {
  wss.clients.forEach(element => {
    element.send(JSON.stringify(obj));
  });
}

wss.on('connection', function (ws) {
  console.log("a client connceting")
  ws.on('message', function (data, flags) {
    if (flags.binary) { return; }
    console.log('>>>: ' + data);
    if (data == 'hide') {
      broadcast({ cmd: 'boss_key', show: 0 })
    } else if (data == 'show') {
      broadcast({ cmd: 'boss_key', show: 1 })
    } else if (data == 'top') {
      broadcast({ cmd: 'bring_to_top' })
    } else if (data == 'closevip') {
      broadcast({ cmd: 'buy_vip_close' })
    } else if (data == 'closepay') {
      broadcast({ cmd: 'pay_close' })
    } else if (data == 'closeweb') {
      broadcast({ cmd: 'web_close' })
    }
  });
  ws.on('close', function () {
    console.log('Connection closed!');
  });
  ws.on('error', function (e) {
  });
});
server.listen(10888);
console.log('Listening on port 10888...');
